<?php
class BlogAction extends AdministratorAction
{
    public function bloglist(){
	    $blog  = M('weibo');
	    $map ="status =1 or status =0";
	    $count = $blog->where($map)->count();
		$p = new Page ( $count, 15 );
	    $bloglist = $blog->where($map)->limit($p->firstRow.','.$p->listRows)->order('utime DESC')->findAll();
		foreach($bloglist as $k=>$v){
		     if(!$bloglist[$k]['title']){
			    $bloglist[$k]['title'] = msubstr(strip_tags($bloglist[$k]['content']),0,100);
			 }
		}
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign('bloglist',$bloglist);
		$this->display();
	}
    public function soBlog(){
        
    }
	public function dodelBlog(){
	    $_POST['weibo_id'] = t($_POST['weibo_id']);
    	$_POST['weibo_id'] = explode(',', $_POST['weibo_id']);
    	$map = $sql = $blog = $attachlist = $blog_ids = array();
		$map['weibo_id'] = array('in',$_POST['weibo_id']);
		$weibo      = M('Weibo');
		$attach     = M('attach');
		$blog_ids   = $weibo->field('blog_id')->where($map)->findAll();
        $attachlist = $attach->field('path')->where($map)->findAll();
		for($i=0;$i<count($attachlist);$i++){
		   @unlink(SITE_PATH.'/'.$attachlist[$i]['path']);
		}
		$attach->where($map)->delete();
		$blog['blog_id'] = array('in',getSubByKey($blog_ids,'blog_id'));
       
		M('Blog')->where($blog)->delete();
    	
		M('Weibo_tag')->where($map)->delete();
    	$res = M('Weibo')->where($map)->delete();
    	if($res) {echo 1;		  }
    	else 	 {echo 0; return ;}
	
	
	}
    public function taglist(){
	    $tag  = M('tag');
	    $count = $tag->count();
		$p = new Page ( $count, 15 );
	    $taglist = $tag->limit($p->firstRow.','.$p->listRows)->order('tag_id DESC')->findAll();
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign('taglist',$taglist);
		$this->display();
	}
   public function dodelTag(){
        $_POST['tag_id'] = t($_POST['tag_id']);
    	$_POST['tag_id'] = explode(',', $_POST['tag_id']);
    	
		$map['tag_id'] = array('in',$_POST['tag_id']);
		
		M('Weibo_tag')->where($map)->delete();
    	$res = M('Tag')->where($map)->delete();
    	if($res) {echo 1;		  }
    	else 	 {echo 0; return ;}
    
   }
   public function addTag(){
    
        $this->display('editTag');
   }
   public function editTag(){
       $tag_id = intval($_GET['tag_id']);
       $tag = M('Tag')->where('tag_id='.$tag_id)->find();
       $this->assign('tag',$tag);
       $this->display();
   }
   public function doAddTag(){
       $_POST['recommend'] = intval($_POST['recommend']);
       $_POST['tag_name']  = t($_POST['tag_name']);
      
       if($_POST['tag_name']==''){
           echo false;
           return ;  
       }
       $tag = M('Tag')->where("tag_name='{$_POST[tag_name]}'")->find();
       if($tag){
          echo 2;
          return ;
       }
       echo ($res = M('Tag')->add($_POST))?$res:0;
   }
   public function doEditTag(){
       $_POST['tag_id']    = intval($_POST['tag_id']);
       $_POST['recommend'] = intval($_POST['recommend']);
       $_POST['tag_name']  = t($_POST['tag_name']);
       if($_POST['tag_name']==''){
        echo false;
        return ;
       }
       echo M('Tag')->where('tag_id='.$_POST['tag_id'])->save($_POST)?1:0;
   }
   /**
    * 推荐标签
    * $aa = 1 推荐 $aa=0 取消推荐
    * 
    */
   public function doRecommendTag(){
        $_POST['tag_id'] = t($_POST['tag_id']);
        $_POST['tag_id'] = explode(',', $_POST['tag_id']);
       	$aa = $_POST['type'] = intval($_POST['type']);  
	    $map['tag_id'] = array('in',$_POST['tag_id']);
		
		//M('Weibo_tag')->where($map)->delete();
    	$res = M('Tag')->where($map)->setField('recommend',$aa);
    	if($res) {echo 1;		  }
    	else 	 {echo 0; return ;}
    
   }
}
?>